package com.yiliao.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.yiliao.service.BannerService;
import com.yiliao.util.DateUtils;
import com.yiliao.util.MessageUtil;
import com.yiliao.util.cache.CacheRemove;

@Service("bannerService")
public class BannerServiceImpl extends ICommServiceImpl implements
		BannerService {

	private MessageUtil mu = null;
	
	@Override
	public MessageUtil getBannerList(int page) {
		try {
			
			String total = "SELECT count(t_id) AS total FROM t_banner ";
			
			Map<String, Object> totalMap = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(total);
			
			String sql = "SELECT t_id,t_img_url,t_link_url,t_is_enable,DATE_FORMAT(t_create_time,'%Y-%m-%d %T') AS t_create_time,t_type FROM t_banner LIMIT ?,5";
			
			List<Map<String, Object>> dataList = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(sql, (page-1)*5);
			
			JSONObject json = new JSONObject();
			
			json.put("total", totalMap.get("total"));
			json.put("rows", dataList);
			
			mu = new MessageUtil();
			mu.setM_istatus(1);
			mu.setM_object(json);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取banner列表异常!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	/*
	 * 点击启用或者禁用(non-Javadoc)
	 * @see com.yiliao.service.BannerService#bannerEnableOrDisable(int)
	 */
	@Override
	@CacheRemove(cacheKey = "#APP_BANNER_LIST#")
	public MessageUtil bannerEnableOrDisable(int t_id) {
		try {
			
			StringBuffer sql = new StringBuffer();
			sql.append("UPDATE t_banner SET ") ;
			sql.append("t_is_enable = CASE t_is_enable  ") ;
			sql.append("  WHEN 0 THEN 1  ") ;
			sql.append("  WHEN 1 THEN 0 ") ;
			sql.append("END ") ;
			sql.append("WHERE t_id = ? ") ;
			
			this.getFinalDao().getIEntitySQLDAO().executeSQL(sql.toString(), t_id);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("启用或者禁用Banner!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	/*
	 * 删除数据(non-Javadoc)
	 * @see com.yiliao.service.BannerService#delBannerById(int)
	 */
	@Override
	@CacheRemove(cacheKey = "#APP_BANNER_LIST#")
	public MessageUtil delBannerById(int t_id) {
		try {
			
			String delSql = "DELETE FROM t_banner WHERE t_id = ?";
			
			this.getFinalDao().getIEntitySQLDAO().executeSQL(delSql, t_id);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除Banner!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	/*
	 * 添加或者修改banner图(non-Javadoc)
	 * @see com.yiliao.service.BannerService#addOrUpdateBanner(java.lang.Integer, java.lang.String, java.lang.String, int)
	 */
	@Override
	@CacheRemove(cacheKey = "#APP_BANNER_LIST#")
	public MessageUtil addOrUpdateBanner(Integer t_id, String t_img_url,
			String t_link_url, int t_is_enable,int t_type) {
		try {
			//新增
			if(null == t_id || 0 == t_id){
				
				String sql = "INSERT INTO t_banner (t_img_url, t_link_url, t_is_enable,t_type, t_create_time) VALUES (?, ?, ?, ?, ?);";
				
				this.getFinalDao().getIEntitySQLDAO().executeSQL(sql, t_img_url,t_link_url,t_is_enable,t_type,DateUtils.format(new Date(), DateUtils.FullDatePattern));
		    //修改
			}else{
				
				String upSql = "UPDATE t_banner set t_img_url=? , t_link_url=?, t_is_enable=?, t_create_time=?,t_type = ?  WHERE t_id=?;";
				
				this.getFinalDao().getIEntitySQLDAO().executeSQL(upSql,t_img_url,t_link_url,t_is_enable,DateUtils.format(new Date(), DateUtils.FullDatePattern),t_type,t_id);
				
			}
			
			mu = new MessageUtil(1, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加或者修改banner!", e);
			mu = new MessageUtil(0, "程序异常!");
		}
		return mu;
	}

	@Override
	public MessageUtil addOrUpdateAdBanner(Integer t_id, String t_ad_table_name, int t_ad_table_type,
			String t_ad_table_content, String t_ad_table_url, String t_ad_table_target, int t_ad_table_sort,
			int t_is_use) {
		try {
			if(null==t_id||t_id==0) {
				//新增
				if(t_ad_table_type==2) {
					if(!(t_ad_table_target.equals("0")
							||t_ad_table_target.equals("1")
							||t_ad_table_target.equals("2")
							||t_ad_table_target.equals("3")
							||t_ad_table_target.equals("4")
							||t_ad_table_target.equals("5")
							||t_ad_table_target.equals("6")
							||t_ad_table_target.equals("7")
							||t_ad_table_target.equals("8")
							||t_ad_table_target.equals("9")
							||t_ad_table_target.equals("10")
							||t_ad_table_target.startsWith("http"))) {
						return new MessageUtil(0,"落地页地址格式错误");
					}
				}
				String sql = "INSERT INTO t_ad_table (t_ad_table_name, t_ad_table_content, t_ad_table_type,"
						+ "t_ad_table_url,t_ad_table_target,"
						+ "t_ad_table_sort,t_is_use, t_create_time) VALUES"
						+ " (?, ?, ?, ?, ?, ?, ?, now());";
				this.executeSql(sql, t_ad_table_name, t_ad_table_content, t_ad_table_type,
						t_ad_table_url,t_ad_table_target,t_ad_table_sort,t_is_use);
				return new MessageUtil(1,"新增成功");
			}else {
				//修改
				String sql = "update t_ad_table set "
						+ "t_ad_table_name=?,"
						+ "t_ad_table_content=?,"
						+ "t_ad_table_type=?,"
						+ "t_ad_table_url=?,"
						+ "t_ad_table_target=?,"
						+ "t_ad_table_sort=?,"
						+ "t_is_use=?"
						+ " where t_id=? ";
				this.executeSql(sql,
						t_ad_table_name,
						t_ad_table_content,
						t_ad_table_type,
						t_ad_table_url,
						t_ad_table_target,
						t_ad_table_sort,
						t_is_use,
						t_id);
				
				return new MessageUtil(1,"修改成功");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0,"服务器繁忙");
		}
	}

	@Override
	public MessageUtil getAdBannerList(int page, int type ) {
		try {
			JSONObject json = new JSONObject();
			StringBuffer sql = new StringBuffer();
			StringBuffer sql2 = new StringBuffer();
			sql.append("select t_id,t_ad_table_name,t_ad_table_content,t_ad_table_type,t_ad_table_url,"
					+ "t_ad_table_target,t_ad_table_sort,t_is_use,DATE_FORMAT(t_create_time, '%Y-%m-%d %H:%i:%S') t_create_time from t_ad_table   ");
			sql2.append("select count(1) total from t_ad_table   ");
			if(type!=0) {
				sql.append(" where t_ad_table_type= "+type);
				sql2.append(" where t_ad_table_type= "+type);
			}
			sql.append(" order by  t_is_use desc,t_ad_table_type,t_ad_table_sort limit ?,10 ");
			List<Map<String,Object>> returnList = this.returnList(sql.toString(),(page-1)*10 );
			Map<String, Object> totalMap = this.returnMap(sql2.toString());
			json.put("total", totalMap.get("total"));
			json.put("rows", returnList);
			mu=new MessageUtil(1,"查询成功");
			mu.setM_object(json);
			return mu;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new MessageUtil(0,"系统繁忙");
		}
	}



	@Override
	public MessageUtil updAdBannerInfo(int t_id, int t_is_use) {
		try {
			
			if(t_is_use==2) {
				String sql="delete from t_ad_table where t_id=?";
				this.executeSql(sql, t_id);
				return new MessageUtil(1,"删除成功");
			}else{
				String sql="update t_ad_table set t_is_use=? where t_id=?";
				this.executeSql(sql,t_is_use, t_id);
				return new MessageUtil(1,"状态修改成功");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0,"系统繁忙");
		}
	}


}
